<?php
require_once 'DatabaseConnection.php';

class InstallHandler
{
	// Skapa databasen.
	public function createDatabase(DatabaseConnection $database, $dbName)
	{
		$query = "CREATE DATABASE ". $dbName. " DEFAULT CHARACTER SET utf8 COLLATE utf8_swedish_ci";
		
		if ($database->Query($query) == true)
		{
			return true;
		}
		
		return false;
	}
	
	// Väljer vilken databas som ska användas.
	public function UseDatabase(DatabaseConnection $database, $dbName)
	{
		$query = "USE ". $dbName ."";
		
		if ($database->Query($query) == true)
		{
			return true;
		}
		
		return false;
	}
	
	// Skapa tabellerna.
	public function createTable(DatabaseConnection $database, $tableName, $usePrefix, $prefix)
	{

		// Kollar om tabellen ska skapas med prefix.
		if ($usePrefix == true)
		{
			$tableName = $prefix.$tableName;
		}
		
		$query = "";
		
		// Kollar vilken tabell som ska skapas.
		if (strpos($tableName, 'notes') !== false)
		{
			// skapar Notes tabellen.
			$query .= "CREATE TABLE IF NOT EXISTS ".$tableName." (
			  `nid` int(11) NOT NULL AUTO_INCREMENT,
			  `notename` varchar(30) COLLATE utf8_swedish_ci NOT NULL,
			  `notecontent` varchar(400) COLLATE utf8_swedish_ci NOT NULL,
			  `notedatetime` datetime NOT NULL,
			  `uid` int(11) NOT NULL,
			  PRIMARY KEY (`nid`)
			) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci";
		}
		
		else if (strpos($tableName, 'users') !== false)
		{
			// Skapar users tabellen.
			$query .= "CREATE TABLE IF NOT EXISTS ".$tableName." (
					  `uid` int(11) NOT NULL AUTO_INCREMENT,
					  `username` varchar(20) COLLATE utf8_swedish_ci NOT NULL,
					  `password` varchar(50) COLLATE utf8_swedish_ci NOT NULL,
					  `admin` varchar(5) COLLATE utf8_swedish_ci NOT NULL,
					  PRIMARY KEY (`uid`)
					) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci";
		}

		if ($database->Query($query) == true)
		{
			return true;
		}
		
		return false;
	}
	
	// Skapa test tabellerna.
	public function createTestNotesTable(DatabaseConnection $database, $tableName)
	{
		$query = "";
		
		// Skapar test_notes
		$query .= "CREATE TABLE IF NOT EXISTS ".$tableName." (
			  `nid` int(11) NOT NULL AUTO_INCREMENT,
			  `notename` varchar(30) COLLATE utf8_swedish_ci NOT NULL,
			  `notecontent` varchar(400) COLLATE utf8_swedish_ci NOT NULL,
			  `notedatetime` datetime NOT NULL,
			  `uid` int(11) NOT NULL,
			  PRIMARY KEY (`nid`)
			) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci";
		
		if ($database->Query($query) == true)
		{
			return true;
		}
		
		return false;
	}
	
	// Skapa test tabellerna.
	public function createTestUsersTable(DatabaseConnection $database, $tableName)
	{
		$query = "";
		
		// Skapar test_notes
		$query .= "CREATE TABLE IF NOT EXISTS ".$tableName." (
					  `uid` int(11) NOT NULL AUTO_INCREMENT,
					  `username` varchar(20) COLLATE utf8_swedish_ci NOT NULL,
					  `password` varchar(50) COLLATE utf8_swedish_ci NOT NULL,
					  `admin` varchar(5) COLLATE utf8_swedish_ci NOT NULL,
					  PRIMARY KEY (`uid`)
					) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci";
		
		if ($database->Query($query) == true)
		{
			return true;
		}
		
		return false;
	}
	
	// Skapar användaren Administrator.
	public function createAdminUser($database, $adminUsername, $adminPassword, $dbPrefix)
	{
		// Sätter isAdmin till yes eftersom användaren ska vara admin.
		$isAdmin = "true";
		
		$query = "INSERT INTO ".$dbPrefix."users (username, password, admin) VALUES ('$adminUsername','$adminPassword','$isAdmin')";
		
		if ($database->Query($query) == true)
		{
			return true;
		}
		
		return false;	
	}
	
	// Skriver allt till filen Settings.php
	public function saveDatabaseSettingsToFile($dbHost, $dbUsername, $dbPassword, $dbName, $tablePrefix)
	{
		$dollarsign = "$";
		
		$xhtml = "<?php
		class Settings
		{
			public ". $dollarsign ."m_dbHost = '$dbHost';
			public ". $dollarsign ."m_dbUsername = '$dbUsername';
			public ". $dollarsign ."m_dbPassword = '$dbPassword';
			public ". $dollarsign ."m_dbName = '$dbName';
			public ". $dollarsign ."m_tablePrefix = '$tablePrefix';
			
			public function getDbHost()
			{
				return ". $dollarsign ."this->m_dbHost;
			}
			
			public function getDbUsername()
			{
				return ". $dollarsign ."this->m_dbUsername;
			}
			
			public function getDbPassword()
			{
				return ". $dollarsign ."this->m_dbPassword;
			}
			
			public function getDbName()
			{
				return ". $dollarsign ."this->m_dbName;
			}
			
			public function getTablePrefix()
			{
				return ". $dollarsign ."this->m_tablePrefix;
			}
		}";
			
		// Skapa filen Settings.php
		$file = fopen("Settings.php", "w+");
		
		// Skriv till filen
		fwrite($file, $xhtml);
		
		// Stäng filen.
		fclose($file);
	}
}